package com.baimsg.microcomponents.service


import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.widget.Toast
import com.baimsg.microcomponents.utils.updateWidget
import java.util.Timer
import kotlin.concurrent.timerTask
import kotlin.random.Random


/**
 * Create by Baimsg on 2023/11/18
 *
 **/
class PocWidgetService : Service() {
    override fun onBind(p0: Intent?): IBinder? = null

    companion object {

        @get:Synchronized
        @set:Synchronized
        var running = false

    }

    private val mTimer: Timer by lazy {
        Timer()
    }

    override fun onCreate() {
        super.onCreate()
        running = true
        toast("服务开始")
        mTimer.schedule(timerTask {
            updateWidget(this@PocWidgetService, Random.nextInt(0, 3))
        }, 0, 1000)
    }

    override fun onDestroy() {
        super.onDestroy()
        running = false
        mTimer.cancel()
        toast("服务停止")
    }

    private fun toast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
}